var UploadCDN = function(){
    return {
        getUploadParam:function(type,callback){
            if(!type){callback(false)}

            $.ajax({
                url:'/assets/request-token?ext_name='+type,
                dataType:'json',
                success:function(data){
                    if(data.success){
                        callback(data.data)
                    }else{
                        callback(false)
                    }
                },
                error:function(){
                    callback(false)
                }
            })
        },
        uploadCDN:function(file,param,callback,progress){
            var xhr = new XMLHttpRequest(),
                upload = xhr.upload;
            
            if(progress){
                upload.addEventListener("progress", progress, false);
            }
            
            xhr.open("POST", 'http://up.qiniu.com', true);
            
            xhr.sendAsBinary(file,param);  
                
            xhr.onload = function() { 
                if (xhr.responseText) {
                    callback(xhr.responseText)
                }
            };
        },
        validFileType:function(type,typeList){
            return _.contains(typeList,type)
        }
    }
};

XMLHttpRequest.prototype.sendAsBinary = function(file,param) {
	var formData = new FormData();
	
	formData.append('key', param.key);
	formData.append('token', param.token);
	formData.append('file', file);
	this.send(formData)
}